//在vue的src目录下建立api文件夹,在api文件夹下建立api.js
//api.js中写
import axios from 'axios';
import qs from 'qs'

const _ajax = (type, url, data) => {

  let Public = { //公共参数  
    'srAppid': ""
  }

  let httpDefaultOpts = { //axios的配置项---可以添加更多的配置  
    method: type,
    url: url,
    params: qs.stringify(data),
    data: qs.stringify(data),
  }

  //判断是get还是post,从而删除httpDefaultOpts中的params或者data中的一个,从而符合axios的配置项
  if (type == 'get') {
    delete httpDefaultOpts.data
  } else {
    delete httpDefaultOpts.params
  }

  let promise = new Promise(function (resolve, reject) {
    axios(httpDefaultOpts).then(
      (res) => {
        resolve(res)
      }
    ).catch(
      (err) => {
        reject(err)
      }
    )

  })
  return promise
}


var fetch = (function () {
  var get = function (url, data) {
    return _ajax('get', url, data)
  }
  var post = function (url, data) {
    return _ajax('post', url, data)
  }
  return {
    get: get,
    post: post
  }
})()



export default {
  goods:function(data){
    return fetch.get("http://jspang.com/DemoApi/oftenGoods.php",data)
  }
}